<?php

namespace app\admin\controller;

use app\common\controller\Base;
use think\Request;

class Admin extends Base
{
    /**
     * 显示资源列表
     */
    public function index()
    {
        $count = db('admin')->count();
        $data = db('admin')->select();
        $this->assign('data', $data);
        $this->assign('count', $count);
        return view();
    }

    /**
     * 显示创建资源表单页.
     */
    public function create()
    {
        $this->RoleSelect();
        return view();
    }

    /**
     * 保存,更新资源
     */
    public function save()
    {
        $data = input('post.');
        $info = model('admin')->addData($data);
        $info['status'] ? _layer_msg($info['msg'],'',1) : jump_page('',$info['msg'],2);
    }

    /**
     * 显示编辑资源表单页.
     */
    public function edit($id)
    {
        $id = input('get.id/d',0);
        $data = db('admin')->alias('a')->field('a.name,r.id,r.title,a.number,a.phone,a.email,a.sex,a.status')->join('role_admin ra','a.id = ra.aid')->join('role r','ra.rid = r.id')->where('a.id',$id)->find();
        $this->assign('id', $id);
        $this->assign('data', $data);
        $this->RoleSelect();
        return view();
    }

    /**
     * 删除指定资源
     */
    public function delete()
    {
        $ids = input('post.id','');
        if(!$ids) return json(['status'=>0,'msg'=>'请选择要删除的管理员!']);
        $info = model('admin')->del($ids);
        return json($info);
    }

    /**
     * 状态操作
     */
    public function status()
    {
        $id = input('post.id', '');
        $status = input('post.status', '');
        return ($status ? db('admin')->where('id', $id)->update(['status' => '0']) : db('admin')->where('id', $id)->update(['status' => '1'])) ? json(['status'=>1]) : json(['status'=>0,'msg'=>'更新状态失败!']);
    }

    /**
     * 角色查询
     */
    private function RoleSelect(){
        $role_list = db('role')->field('id,title')->order('id desc')->select();
        $this->assign('role_list', $role_list);
    }
}
